package com.diligent.cloud.sfc.factory.impl;

import com.alibaba.fastjson.JSON;
import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.canny.commons.page.IPage;
import com.diligent.cloud.logistics.container.model.LcmBoxLedger;
import com.diligent.cloud.sfc.factory.StationFactory;
import com.diligent.cloud.sfc.model.detail.BoxDetailInfo;
import com.diligent.cloud.sfc.model.info.BoxInfo;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkShopExtrusionService;
import com.diligent.cloud.workorder.model.WoOperationCompleteDetail;
import com.diligent.cloud.workorder.model.WoOrder;
import com.google.common.base.Preconditions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * BoxStation class
 *
 * @author xf107278
 * @date 3/31/2020
 */
@Component
public class BoxStation implements StationFactory {

    private ISfcService sfcService;

    private final WorkShopExtrusionService workShopExtrusionService;


    public BoxStation(ISfcService sfcService, WorkShopExtrusionService workShopExtrusionService) {
        this.sfcService = sfcService;
        this.workShopExtrusionService = workShopExtrusionService;
    }

    @Override
    public Result getOrderInfo(WoOrder order) {

        String prodBach = sfcService.getProdBachByOrder(order);
        Preconditions.checkNotNull(prodBach, "查询不到此[%s]工单得批次号", prodBach);
        BoxDetailInfo boxDetailInfo = new BoxDetailInfo();
        //查询到同批次已完成剪切但是未挂料/装框的工单
        ResponseEntity<ListResult> orderList =
                sfcService.getSameOrderByProdBatch(prodBach
                        , order.getMachineCode()
                        , order.getSystemId()
                        , order.getOrderType()
                        , 0, 1);
        boxDetailInfo.setWaitTotalPcs(0L);
        Long waitTotalPcs = 0L;
        //总完成数
        Long completedTotalPcs = 0L;
        //未完成的工单的已报数的数量-未完成工单已剪切出来的数量=等待装框的数量
        if (orderList.getBody()!=null && orderList.getBody().getData()!=null){
            for (LinkedHashMap map : (List<LinkedHashMap>) orderList.getBody().getData()) {
                WoOrder woOrder = JSON.parseObject(JSON.toJSONString(map), WoOrder.class);
                //当前工单已经装框的数

                WoOperationCompleteDetail operationCompleteDetailByWoOrder =
                        sfcService.getOperationCompleteDetailByWoOrder(woOrder.getWoOrder());

                //剪切出来的数
                if (operationCompleteDetailByWoOrder != null) {
                    Integer completeDigit = getCompleteDigitByOrderId(woOrder.getId());
                    completedTotalPcs += completeDigit;
                    waitTotalPcs += operationCompleteDetailByWoOrder.getDigit().longValue() - completeDigit;
                }
            }
        }
        boxDetailInfo.setWaitTotalPcs(waitTotalPcs);
        boxDetailInfo.setSameBatchWaitOrder(orderList.getBody().getData().size());
        //查询到当前批次已完成装框/挂料工单
        orderList =
                sfcService.getSameOrderByProdBatch(prodBach
                        , order.getMachineCode()
                        , order.getSystemId()
                        , order.getOrderType()
                        , 1, 1);
        if (orderList.getBody()!=null && orderList.getBody().getData()!=null){
            boxDetailInfo.setSameBatchOrderCompleted(orderList.getBody().getData().size());
            //总装框数等于已完成工单加未完成工单的装卸单数量
            for (LinkedHashMap orderMap : (List<LinkedHashMap>) orderList.getBody().getData()) {
                WoOrder woOrder = JSON.parseObject(JSON.toJSONString(orderMap), WoOrder.class);
                completedTotalPcs += getCompleteDigitByOrderId(woOrder.getId());
            }
            boxDetailInfo.setCompletedTotalPcs(completedTotalPcs);
        }

        IPage<LcmBoxLedger> aNull = sfcService.getIdleBox("null", 1L, 10000L);
        if (aNull != null) {
            List<LcmBoxLedger> idleBox = aNull.getRecords();
            boxDetailInfo.setLoadingQuantityPerBox(idleBox.stream().findFirst().get().getMatTotalLotDigit().longValue());
        } else {
            boxDetailInfo.setLoadingQuantityPerBox(0L);
        }

        ListResult result = workShopExtrusionService.boxReportedFilter(null, order.getId().toString(), false);
        Integer totalDigit = 0;
        if (result.getData().size() != 0) {
            List<BoxInfo> boxInfos = result.getData();
            for (BoxInfo boxInfo : boxInfos) {
                totalDigit += boxInfo.getDigit().intValue();
            }
        }
        boxDetailInfo.setInBoxTotalPcs(totalDigit);
        boxDetailInfo.setUsedBox(sfcService.getLogisticsTransPortInfo(order.getMachineCode()).getRecords().size());


        IPage<LcmBoxLedger> idleBox = sfcService.getIdleBox(order.getMachineCode(), 1L, 10000L);
        if (idleBox != null) {
            boxDetailInfo.setAvailableBox(idleBox.getTotal().intValue());
        } else {
            boxDetailInfo.setAvailableBox(0);
        }
        boxDetailInfo.setRequireDigit(order.getDeliveryDigit().longValue());

        WoOperationCompleteDetail operationCompleteDetailByWoOrder =
                sfcService.getOperationCompleteDetailByWoOrder(order.getWoOrder());
        if (operationCompleteDetailByWoOrder != null) {
            boxDetailInfo.setCompleteDigit(sfcService.getOperationCompleteDetailByWoOrder(order.getWoOrder()).getDigit().longValue());
        } else {
            boxDetailInfo.setCompleteDigit(0L);
        }

        return new ObjectResult<>(boxDetailInfo);
    }

    protected Integer getCompleteDigitByOrderId(Long orderId) {
        ListResult result = workShopExtrusionService.boxReportedFilter(null, orderId.toString(), true);
        int totalDigit = 0;
        if (result.getData().size() != 0) {
            List<BoxInfo> boxInfos = result.getData();
            for (BoxInfo boxInfo : boxInfos) {
                totalDigit += boxInfo.getDigit().intValue();
            }
        }
        return totalDigit;
    }

}
